package com.qingpong.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qingpong.reggie.common.BaseContext;
import com.qingpong.reggie.common.R;
import com.qingpong.reggie.dto.OrdersDto;
import com.qingpong.reggie.entity.OrderDetail;
import com.qingpong.reggie.entity.Orders;
import com.qingpong.reggie.entity.ShoppingCart;
import com.qingpong.reggie.service.OrderDetailService;
import com.qingpong.reggie.service.OrdersService;
import com.qingpong.reggie.service.ShoppingCartService;
import com.qingpong.reggie.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/order")
@Slf4j
public class OrdersController {
    @Autowired
    private OrdersService ordersService;
    @Autowired
    private UserService userService;
    @Autowired
    private ShoppingCartService shoppingCartService;
    @Autowired
    private OrderDetailService orderDetailService;

    @GetMapping("/page")
    public R<Page> page(int page, int pageSize, Long number, String beginTime, String endTime) {
        log.info("参数接受{},{},{}", number, beginTime, endTime);
        Page<Orders> ordersPage = new Page<>(page, pageSize);
        Page<OrdersDto> ordersDtoPage = new Page<>();

        LambdaQueryWrapper<Orders> wrapper = new LambdaQueryWrapper<>();
        wrapper.between(StringUtils.isNotEmpty(beginTime), Orders::getOrderTime, beginTime, endTime);
        wrapper.eq(number != null, Orders::getNumber, number);
        wrapper.orderByDesc(Orders::getOrderTime);
        ordersService.page(ordersPage, wrapper);
        BeanUtils.copyProperties(ordersPage, ordersDtoPage, "records");

        List<Orders> records = ordersPage.getRecords();
        List<OrdersDto> list = records.stream().map((item) -> {


            item.setUserName(item.getConsignee());

            OrdersDto ordersDto = new OrdersDto();

            BeanUtils.copyProperties(item, ordersDto);

            return ordersDto;
        }).collect(Collectors.toList());
        ordersDtoPage.setRecords(list);
        return R.success(ordersDtoPage);
    }

    /**
     * 用户下单
     *
     * @param orders
     * @return
     */
    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders orders) {
        log.info("订单数据：{}", orders);
        ordersService.submit(orders);
        return R.success("下单成功");
    }

    /**
     * 分页查询     * @param page
     *
     * @param pageSize
     * @return
     */
    @GetMapping("/userPage")
    public R<Page> userPage(int page, int pageSize) {
        Page<Orders> ordersPage = new Page<>(page, pageSize);
        Page<OrdersDto> OrdersDto = new Page<>();

        LambdaQueryWrapper<Orders> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Orders::getUserId, BaseContext.getCurrentId());
        wrapper.orderByDesc(Orders::getOrderTime);
        ordersService.page(ordersPage, wrapper);

        BeanUtils.copyProperties(ordersPage, OrdersDto, "records");

        List<Orders> records = ordersPage.getRecords();

        List<OrdersDto> list = records.stream().map((item) -> {
            Long id = item.getId();
            LambdaQueryWrapper<OrderDetail> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(OrderDetail::getOrderId, id);
            List<OrderDetail> orderDetails = orderDetailService.list(queryWrapper);

            OrdersDto ordersDto = new OrdersDto();

            BeanUtils.copyProperties(item, ordersDto);

            ordersDto.setOrderDetails(orderDetails);
            return ordersDto;

        }).collect(Collectors.toList());
        OrdersDto.setRecords(list);
        return R.success(OrdersDto);

    }

    /**
     * 改状态
     *
     * @param orders
     * @return
     */
    @PutMapping
    public R<String> status(@RequestBody Orders orders) {
        log.info("订单 状态{}", orders);
        Orders byId = ordersService.getById(orders);
        byId.setStatus(orders.getStatus());

        ordersService.updateById(byId);
        return R.success("修改成功");
    }

    /**
     * 再来一单
     * @param orderDetail
     * @return
     */
    @PostMapping("/again")

    public R<List<ShoppingCart>> again(@RequestBody OrderDetail orderDetail) {

        LambdaQueryWrapper<OrderDetail> wrapper = new LambdaQueryWrapper<>();

        wrapper.eq(OrderDetail::getOrderId, orderDetail.getId());
        List<OrderDetail> list = orderDetailService.list(wrapper);
        List<ShoppingCart> list1 = list.stream().map((item) -> {
            ShoppingCart shoppingCart = new ShoppingCart();
            BeanUtils.copyProperties(item, shoppingCart);
            shoppingCart.setUserId(BaseContext.getCurrentId());
            return shoppingCart;
        }).collect(Collectors.toList());
        shoppingCartService.saveBatch(list1);
        return R.success(list1);
    }
}
